﻿#pragma once
#include "zyObject.h"

namespace BlackMoonUI {
    // 静态工厂方法
    zyObject* zyObject::Create() {
        return new zyObject();
    }

    // 构造函数
    zyObject::zyObject()
        : m_pSelf(this),
        m_refCount(1),
        m_fnWndProc(nullptr),
        m_fnChildWndProc(nullptr),
        m_fnDestroy(nullptr),
        m_pPtrData(std::make_unique<std::unordered_map<std::wstring, LPVOID>>()),
        m_pStringData(std::make_unique<std::unordered_map<std::wstring, std::wstring>>()),
        m_pRegEvents(std::make_unique<std::unordered_map<std::wstring, TRegEvent*>>()) {}

    // 析构函数
    zyObject::~zyObject() {
        m_pSelf = nullptr;
        releaseEventMap();
        // unique_ptr 自动释放容器内存
    }

    // 释放事件映射表
    void zyObject::releaseEventMap() {
        if (!m_pRegEvents) return; // 确保 m_pRegEvents 不为空

        for (auto it = m_pRegEvents->begin(); it != m_pRegEvents->end(); ) {
            TRegEvent* pRegEvent = it->second;
            if (pRegEvent) {
                delete pRegEvent;
            }
            it = m_pRegEvents->erase(it); // erase 返回下一个有效的迭代器
        }
    }

    // 窗口消息处理
    void zyObject::setWndProc(FnWndProc fnWndProc) {
        m_fnWndProc = fnWndProc;
    }

    void zyObject::setChildWndProc(FnChildWndProc fnChildWndProc) {
        m_fnChildWndProc = fnChildWndProc;
    }

    void zyObject::setDestroy(FnDestroy fnDestroy) {
        m_fnDestroy = fnDestroy;
    }

    LRESULT zyObject::callWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle) {
        return m_fnWndProc ? m_fnWndProc(this, hWnd, uMsg, wParam, lParam, bHandle) : 0;
    }

    LRESULT zyObject::callChildWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle) {
        return m_fnChildWndProc ? m_fnChildWndProc(this, hWnd, uMsg, wParam, lParam, bHandle) : 0;
    }

    void zyObject::callDestroy() {
        if (m_fnDestroy) m_fnDestroy(this);
    }

    // 引用计数
    size_t zyObject::getRefCount() const {
        return m_refCount.load(std::memory_order_acquire);
    }

    size_t zyObject::addRef() {
        return m_refCount.fetch_add(1, std::memory_order_release);
    }

    size_t zyObject::release() {
        size_t count = m_refCount.fetch_sub(1, std::memory_order_acquire);
        if (count == 0) {
            if (m_fnDestroy)
                m_fnDestroy(this);
            delete this;
        }
        return count;
    }

    // 数据存储
    bool zyObject::hasPtrData(const std::wstring& key) const {
        return m_pPtrData->find(key) != m_pPtrData->end();
    }

    void zyObject::setPtrData(const std::wstring& key, void* data) {
        (*m_pPtrData)[key] = data;
    }

    void* zyObject::getPtrData(const std::wstring& key) const {
        auto it = m_pPtrData->find(key);
        return it != m_pPtrData->end() ? it->second : nullptr;
    }

    void zyObject::removePtrData(const std::wstring& key) {
        m_pPtrData->erase(key);
    }

    void zyObject::setStringData(const std::wstring& key, const std::wstring& data) {
        (*m_pStringData)[key] = data;
    }

    std::wstring zyObject::getStringData(const std::wstring& key) const {
        auto it = m_pStringData->find(key);
        return it != m_pStringData->end() ? it->second : L"";
    }

    void zyObject::removeStringData(const std::wstring& key) {
        m_pStringData->erase(key);
    }

    // 事件注册
    TRegEvent* zyObject::getRegEvent(const std::wstring& key) const {
        auto it = m_pRegEvents->find(key);
        return it != m_pRegEvents->end() ? it->second : nullptr;
    }

    void zyObject::setRegEvent(const std::wstring& key, TRegEvent* pRegEvent) {
        TRegEvent* pOldEvent = getRegEvent(key);
        if (pOldEvent) delete pOldEvent;
        (*m_pRegEvents)[key] = pRegEvent;
    }

    // 自检
    BOOL zyObject::checkSelf() const {
        return m_pSelf == this;
    }
}
